package org.bulu.project.web.controller;


import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.bulu.project.common.result.Result;
import org.bulu.project.system.model.dto.FileInfo;
import org.bulu.project.system.service.OssService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件控制层
 *
 * @author Ray
 * @since 2022/10/16
 */
@RestController
@RequestMapping("/api/v1/files")
@RequiredArgsConstructor
public class FileController {

    private final OssService ossService;

    @PostMapping
    public Result<FileInfo> uploadFile(
            @RequestParam(value = "file") MultipartFile file
    ) {
        FileInfo fileInfo = ossService.uploadFile(file);
        return Result.success(fileInfo);
    }

    @DeleteMapping
    @SneakyThrows
    public Result<?> deleteFile(
            @RequestParam String filePath
    ) {
        boolean result = ossService.deleteFile(filePath);
        return Result.judge(result);
    }
}
